﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MailSlot;

namespace GurLoaderHelper
{
    public class MailslotMessageSender : IMessageSender
    {
        private string MailslotName { get; set; }

        public MailslotMessageSender(string mailslotName)
        {
            MailslotName = mailslotName;
        }

        public void SendMessage(string message)
        {
            using (StreamWriter w = File.AppendText("log.txt"))
            {
                Log(string.Format("Processing message: {0}", message), w);
            }

            using (MailslotClient client = new MailslotClient(MailslotName))
            {
                client.SendMessage(message);
                MessageBox.Show("Success! Send message " + message);
            }
        }

        public static void Log(string logMessage, TextWriter w)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine("-------------------------------");
        }

    }
}
